package com.turnengine.client.local.alliance.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Request To Join Alliance Data Serializer.
 */
public class RequestToJoinAllianceDataSerializer extends AbstractCommandDataSerializer<Boolean> implements IRequestToJoinAllianceDataSerializer {

	/**
	 * Creates a new Request To Join Alliance Data Serializer.
	 */
	public RequestToJoinAllianceDataSerializer() {
		super(RequestToJoinAlliance.COMMAND_ID);
	}

	@Override
	public Boolean readReturnValue(IDataReader reader) throws IOException {
		Boolean value = reader.readBooleanObject(true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, Boolean value) throws IOException {
		writer.writeBooleanObject(value, true);
	}

	@Override
	public IExecutableCommand<Boolean> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int allianceId = reader.readInt();

		// Create object
		RequestToJoinAlliance object = new RequestToJoinAlliance();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setAllianceId(allianceId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<Boolean> argument) throws IOException {
		RequestToJoinAlliance object = (RequestToJoinAlliance) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int allianceId = object.getAllianceId();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(allianceId);
	}

	@Override
	public IExecutableCommandResponse<Boolean> newResponse() {
		return new RequestToJoinAllianceResponse();
	}
}
